const mysql = require('mysql2/promise');

async function testDatabase() {
  const connection = await mysql.createConnection({
    host: 'localhost',
    port: 3306,
    user: 'root',
    password: 'lj.960427',
    database: 'Yuheng-Admin'
  });

  try {
    console.log('✅ 数据库连接成功');

    // 测试查询用户表
    const [rows] = await connection.execute('SHOW TABLES');
    console.log('📋 数据库中的表:', rows);

    // 查询用户表结构
    const [userTable] = await connection.execute('DESCRIBE users');
    console.log('🏗️ 用户表结构:', userTable);

    // 查询用户数据
    const [users] = await connection.execute('SELECT id, username, email, nickname, role, isActive, LENGTH(password) as password_length FROM users');
    console.log('👥 用户数据:', users);

    // 测试查询admin用户
    const [adminUser] = await connection.execute('SELECT * FROM users WHERE username = ?', ['admin']);
    if (adminUser.length > 0) {
      console.log('🔍 admin用户详情:', {
        id: adminUser[0].id,
        username: adminUser[0].username,
        email: adminUser[0].email,
        password_length: adminUser[0].password.length,
        role: adminUser[0].role,
        isActive: adminUser[0].isActive
      });
    } else {
      console.log('❌ admin用户不存在');
    }

  } catch (error) {
    console.error('❌ 数据库操作失败:', error.message);
  } finally {
    await connection.end();
  }
}

testDatabase();
